﻿Imports System.Windows.Forms
Imports UCBase
Imports System.Drawing

Public Class FullScreen
    Implements UCBase.I_LoadSave
    Implements UCBase.I_EditMode
    Private Pic As PictureBox
    Private f As Form
    Private SelScreen As Integer
    Private BackPic As New BackgroundCreator
    Private Mover As New Mover
    Private M_Edit As Boolean = True
    Private WithEvents ImageInput As I_ImageExchange
    Private Zoomed As Boolean = False
    Private screencol As New Collection
    Private PT As Integer
    Private AC As Integer = 0
    Private AV As Integer = 0
    Private FPS As Integer

    Public Property EditMode() As Boolean
        Get
            Return M_Edit
        End Get
        Set(ByVal value As Boolean)
            M_Edit = value
            Mover.EditMode = value
        End Set
    End Property
    Private Sub Socket2_Connect() Handles Socket2.Connect
        If Socket2.SocketType = Socket.Socket_Type.Image Then
            Dim S As Socket = Socket2.Remote
            ImageInput = S.Parent
        End If
    End Sub

    Private Sub ImageInput_ImageOut(ByVal Image As System.Drawing.Bitmap) Handles ImageInput.ImageOut1
        If Image IsNot Nothing Then
            Dim TBMP As New Bitmap(Image)
            Dim G As Graphics = Graphics.FromImage(TBMP)
            Dim CT As Integer = My.Computer.Clock.TickCount
            Dim C As Integer = CT - PT
            PT = CT
            If C > 0 Then
                AV = AV + (1000 / C)
                AC = AC + 1
                If AC = 10 Then
                    AC = 0
                    FPS = AV / 10
                    AV = 0
                End If
            End If
            Dim S As String = Image.Width.ToString & " x " & Image.Height.ToString & " - " & Image.PixelFormat.ToString.Substring(6) & " " & FPS.ToString("00") & " FPS"
            G.DrawString(S, Label2.Font, Brushes.White, 10, 10)
            Pic.Image = TBMP
            'TBMP.Dispose()
        End If
    End Sub

    Private Sub Socket2_Disconnected() Handles Socket2.Disconnected
        If Socket2.SocketType = Socket.Socket_Type.Image Then
            ImageInput = Nothing
        End If
        Socket2.SocketType = Socket.Socket_Type.Universal
    End Sub


    Private Sub FullScreen_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        '142, 119 162, 295
        Me.Width = 160
        Me.Height = 165

    End Sub
    Private Sub MakeWin()
        f = New Form
        Pic = New PictureBox
        f.Controls.Add(Pic)
        Pic.Dock = DockStyle.Fill
        Pic.SizeMode = PictureBoxSizeMode.Zoom
        Pic.BackColor = Color.Black
        f.Width = 200
        f.Height = 200
        f.Text = Me.Name
        f.Show()
    End Sub
    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.BackgroundImage = BackPic.CreatePic(Me.Width, Me.Height)
        Mover.Connect(Me)
        Label3.Text = Me.Name
        MakeWin()
        For Each S As Screen In Screen.AllScreens
            screencol.Add(S)
        Next

        If screencol.Count > 1 Then
            RadioButton2.Enabled = True
            RadioButton2.Checked = True
            SetScreen(2)
            SelScreen = 2
        Else
            RadioButton1.Enabled = True
            RadioButton1.Checked = True
            SetScreen(1)
            SelScreen = 1
        End If

        For c = 1 To screencol.Count
            Dim RN As String = "RadioButton" & c.ToString
            Dim R As Array = Me.Controls.Find(RN, False)
            Dim RB As RadioButton = R(0)
            AddHandler RB.CheckedChanged, AddressOf RCheckedChanged
            RB.Enabled = True
        Next
    End Sub

    Private Sub RCheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Dim N As String = sender.name
        Dim I As Integer = Val(N.LastOrDefault)
        Label3.Text = "Screen " & I.ToString
        SelScreen = I
        SetScreen(I)
    End Sub
    Private Sub SetScreen(ByVal Index As Integer)
        Dim S As Screen = screencol.Item(Index)
        f.Bounds = S.Bounds
        f.FormBorderStyle = FormBorderStyle.None
    End Sub

    Public Function GetSettings() As String Implements UCBase.I_LoadSave.GetSettings
        Return SelScreen.ToString
    End Function

    Public Sub SetSettings(ByVal Settings As String) Implements UCBase.I_LoadSave.SetSettings
        SetScreen(Val(Settings))
        Label3.Text = "Screen " & Settings
    End Sub

    Public Sub SetEditMode(ByVal Mode As UCBase.Diagram.EditMode) Implements UCBase.I_EditMode.SetEditMode

    End Sub
End Class
